package com.biwano.swapmyjob.mail;

import com.biwano.swapmyjob.beans.BaseBean;
import com.biwano.swapmyjob.tools.StringMaker;
import com.biwano.swapmyjob.tools.UrlHelper;


public abstract class MailSender {


	private static final String MAILBUNDLE_SUFFIX = "Mail";
	private static final String MAILBUNDLE_SUBJECT_KEY = "subject";
	private static final String MAILBUNDLE_TEXT_KEY = "text";

	private static String getMailData(String mailTemplateName, String key) {
		return (String) BaseBean.getBundleText(mailTemplateName + MAILBUNDLE_SUFFIX, key);
	}

	private static String getMailBundleString(String mailTemplateName,
			String key, String[][] allParams) {

		String originalString = getMailData(mailTemplateName, key);
		
		StringMaker stringMaker = new StringMaker(originalString);
		stringMaker.setParameters(allParams);
		stringMaker.setParameter("swapmyjob_baseurl",UrlHelper.getBaseUrl());
		return stringMaker.getString();
	}

	protected static void sendMail(MailConnectionManager connection, String recipient, String mailTemplateName, String[][] allParams) {

			String subject = getMailBundleString(mailTemplateName,
					MAILBUNDLE_SUBJECT_KEY, allParams);
			String text = getMailBundleString(mailTemplateName, MAILBUNDLE_TEXT_KEY,
					allParams);

			connection.send(recipient, subject, text);

	}
}
